package main

import (
	"fmt"
	"github.com/gogap/logs"
	"yundian/internel/app/logic"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

func main() {
	//dingo.InitViper("./configs/adm.yaml")
	//dingo.InitViper("./configs/zmtest-adm.yaml")
	dingo.InitViper("./configs/zhongman.yaml")
	dbs.NewMysqlComponent().Step()

	var tradeNoList []string
	var userId int

	placeId := 446332
	Sql := `SELECT user_id FROM places WHERE id=?`
	if db := global.Orm.Debug().Raw(Sql, placeId).Find(&userId); db.Error != nil {
		logs.Error(db.Error)
		return
	}

	tradeNoSql := `SELECT trade_no FROM orders WHERE place_id=? AND trade_no>'ZM20221201'`
	if db := global.Orm.Debug().Raw(tradeNoSql, placeId).Find(&tradeNoList); db.Error != nil {
		logs.Error(db.Error)
		return
	}

	if db := global.Orm.Debug().Exec("DELETE FROM orders_user_relevancy WHERE trade_no IN (?)", tradeNoList); db.Error != nil {
		logs.Error(db.Error)
		return
	}
	if db := global.Orm.Debug().Exec("DELETE FROM dividends_log WHERE trade_no IN (?)", tradeNoList); db.Error != nil {
		logs.Error(db.Error)
		return
	}
	if db := global.Orm.Debug().Exec(fmt.Sprintf("UPDATE orders SET user_id=%d WHERE trade_no IN (?)", userId), tradeNoList); db.Error != nil {
		logs.Error(db.Error)
		return
	}

	var orderListUse []logic.OrderListUseType
	orderSql := `SELECT o.*,p.branch_user_id,DATE_FORMAT(FROM_UNIXTIME(o.end_at), '%Y-%m-%d')end_date,DATE_FORMAT(
	FROM_UNIXTIME(o.end_at), '%Y-%m')end_month FROM orders o LEFT JOIN places p ON o.parent_id=p.id WHERE o.trade_no in (?) 
	ORDER BY o.id`
	if db := global.Orm.Raw(orderSql, tradeNoList).Find(&orderListUse); db.Error != nil {
		logs.Error(db.Error)
		return
	}

	logic.NewTaskLogic().OrderRelevanceSyncDataExec(orderListUse, 300)
	//OrderRelevanceSyncDataExec(orderListUse, 300)
}
